-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.c
90 lines (75 loc) · 1.57 KB
/
Solution.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
typedef struct Stack {
int arr[MAX];
int top;
} Stack;
void initStack(Stack* s) {
s->top = -1;
}
int isEmpty(Stack* s) {
return s->top == -1;
}
int isFull(Stack* s) {
return s->top == MAX - 1;
}
void push(Stack* s, int value) {
if (isFull(s)) {
printf("Stack Overflow\n");
return;
}
s->arr[++s->top] = value;
}
int pop(Stack* s) {
if (isEmpty(s)) {
printf("Stack Underflow\n");
return -1;
}
return s->arr[s->top--];
}
typedef struct Queue {
Stack s1, s2;
} Queue;
void initQueue(Queue* q) {
initStack(&q->s1);
initStack(&q->s2);
}
void enqueue(Queue* q, int value) {
push(&q->s1, value);
}
int dequeue(Queue* q) {
if (isEmpty(&q->s2)) {
while (!isEmpty(&q->s1)) {
push(&q->s2, pop(&q->s1));
}
}
return pop(&q->s2);
}
int main() {
Queue q;
initQueue(&q);
int choice, value;
do {
printf("\n1. Enqueue\n2. Dequeue\n3. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to enqueue: ");
scanf("%d", &value);
enqueue(&q, value);
break;
case 2:
value = dequeue(&q);
if (value != -1)
printf("Dequeued value: %d\n", value);
break;
case 3:
printf("Exiting...\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 3);
return 0;
}